<template>
	<view class="container">
		<view class="content">
			<image class="docterImg" :src="docter.avatar"></image>
			<view class="right-aside">
				<view class="name">
					{{docter.nickname}}
					<text class="jjtips">
						{{docter.userRank=="1"?"实习医生":docter.userRank=="2"?"主治医生":docter.userRank=="3"?"主任医生":"其他医生"}}
					</text>
				</view>
				<text class="tips">
					{{docter.introduction}} --- {{docter.userRank=="1"?"实习医生":docter.userRank=="2"?"主治医生":docter.userRank=="3"?"主任医生":"其他医生"}}
				</text>
			</view>
		</view>
		<view class="jzForm">
			<view class="formItem">
				<text class="formLabel">就诊科室</text>
				<view class="formInput">{{deptName}}</view>
			</view>
			<view class="formItem">
				<text class="formLabel">就诊患者</text>
				<view class="formInput">{{patientInfo.name}}</view>
			</view>
			<view class="formItem">
				<text class="formLabel">预约日期</text>
				<view class="formInput">
					<picker mode="date" :value="date" :start="startDate" :end="endDate"
						@change="bindDateChange">
						<view class="uni-input">{{date}}</view>
					</picker>
				</view>
			</view>
			<view class="formItem">
				<text class="formLabel">挂号时段</text>
				<view class="formInput">
					<picker class="picker" @change="bindPickerChange" :value="subsectionTypeIndex" :range="subsectionTypes" range-key="label">
						<view class="uni-input pickerValue">{{subsectionTypes[subsectionTypeIndex].label}}</view>
					</picker>
				</view>
			</view>
			<view class="formItem">
				<text class="formLabel">挂号类型</text>
				<view class="formInput">
					<picker class="picker" @change="bindPickerChange1" :value="schedulingTypeIndex" :range="schedulingTypes" range-key="label">
						<view class="uni-input pickerValue">{{schedulingTypes[schedulingTypeIndex].label}}</view>
					</picker>
				</view>
			</view>
			<view class="formItem">
				<text class="formLabel">挂号项目</text>
				<view class="formInput">
					<picker class="picker" @change="bindPickerChange2" :value="projectTypeIndex" :range="projectTypes" range-key="label">
						<view class="uni-input pickerValue">{{projectTypes[projectTypeIndex].label}}</view>
					</picker>
				</view>
			</view>
			<view class="formItem">
				<text class="formLabel">挂号费用</text>
				<view class="formInput">￥{{Fee}}.00元</view>
			</view>
		</view>
		<view class="button-tips">
			<view class="title-tip">温馨提示：</view>
			<view class="jztips">1、预约成功后可在历史病例查看预约详情</view>
			<view class="jztips">2、如因个人原因不能在规定时间到达医院，请及时取消预约订单</view>
		</view>
		<view class="btn-group">
			<view class="left-aside">总金额:￥{{Fee}}.00元</view>
			<view class="right-content" @click="yygh">
				<text>预约挂号</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserById
	} from '../../api/user/index.js';
	import {
		listDeptOptions
	} from '../../api/dept/index.js'
	import {
		getDictOptions
	} from '../../api/dict/index.js'
	import {
		addRegistration,collectFee
	} from '../../api/registration/index.js'
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				date: currentDate,
				docter: {},
				deptName: '',
				patientInfo: {},
				subsectionTypeIndex: 0,
				schedulingTypeIndex: 0,
				projectTypeIndex: 1,
				subsectionTypes: [],
				schedulingTypes: [],
				projectTypes: [],
				schedulingTypeFee: 6,
				projectTypeFee: 15,
				Fee: 0,
				registrationFormDto: {
					patientDTO: this.patientInfo,
					registrationDto: this.registrationDto,
				},
				registrationDto: {
				  userId: uni.getStorageSync('docter').id,
				  doctorName: uni.getStorageSync('docter').nickname,
				  deptId: "",
				  schedulingType: "",
				  subsectionType: "",
				  visitDate: "",
				  regItemId: "",
				  regItemAmount: 0,
				},
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		async onLoad(options){
			getUserById(options.id).then(res => {
				this.docter=res.data
			})
			listDeptOptions().then(res=>{
				this.deptName=res.data.find(item => item.value == uni.getStorageSync('docter').deptId).label
			})
			getDictOptions('regItemTime').then(res=>{
				this.subsectionTypes=res.data
			})
			getDictOptions('registeredItem').then(res=>{
				this.schedulingTypes=res.data
			})
			getDictOptions('projectType').then(res=>{
				this.projectTypes=res.data
			})
			this.patientInfo=uni.getStorageSync('patient');
			this.calculateFee();
		},
		methods: {
			yygh(){
				uni.showModal({
					title: '提示',
					content: '确定要预约吗？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.registrationDto.deptId=uni.getStorageSync('docter').deptId
							this.registrationFormDto.patientDTO=this.patientInfo
							this.registrationDto.schedulingType=this.schedulingTypes[this.schedulingTypeIndex].value
							this.registrationDto.subsectionType=this.subsectionTypes[this.subsectionTypeIndex].value
							this.registrationDto.visitDate=this.date
							this.registrationDto.regItemId=1
							this.registrationDto.regItemAmount=this.Fee
							this.registrationFormDto.registrationDto=this.registrationDto
							addRegistration(this.registrationFormDto).then(res=>{
								if(res.data != null){
									const regId=res.data
									uni.showToast({
										title: '预约成功',
										icon: 'success',
										duration: 2000
									})
									setTimeout(()=>{
										uni.showModal({
											title: '提示',
											content: '是否立即缴费？',
											success: (res) => {
												if (res.confirm) {
													console.log('用户点击确定');
													collectFee(regId).then(res=>{
														if(res.data == null){
															uni.showToast({
																title: '缴费成功',
																icon: 'success',
																duration: 2000
															})
															setTimeout(()=>{
																uni.reLaunch({
																	url: '/pages/registration/index'
																})
															},1000)
														}else{
															uni.showToast({
																title: '缴费失败,请稍后再试',
																icon: 'error',
																duration: 2000
															})
														}
													})
												} else if (res.cancel) {
													uni.showToast({
														title: '缴费已取消',
														icon: 'error',
														duration: 2000
													})
													setTimeout(()=>{
														uni.reLaunch({
															url: '/pages/registration/index'
														})
													},1000)
												}
											},
										})
									},1000)
								}else{
									uni.showToast({
										title: '预约失败,请稍后再试',
										icon: 'error',
										duration: 2000
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					},
				})
			},
			//计算挂号费用
			calculateFee() {
				// 根据选择的挂号类型和挂号项目计算费用
				// 这里只是一个示例，实际计算逻辑可能更复杂
				this.Fee = this.schedulingTypeFee + this.projectTypeFee;
			},
			bindPickerChange: function (e) {
			    console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.subsectionTypeIndex = e.detail.value
			},
			bindPickerChange1: function (e) {
				this.schedulingTypeIndex = e.detail.value
				const schedulingType = this.schedulingTypes[this.schedulingTypeIndex];
				if (schedulingType.value == 1) {
					this.schedulingTypeFee = 6;
				} else if(schedulingType.value == 2){
					this.schedulingTypeFee = 7;
				} else if(schedulingType.value == 3){
					this.schedulingTypeFee = 12;
				} else if(schedulingType.value == 4){
					this.schedulingTypeFee = 13;
				} 
				this.calculateFee();
			},
			bindPickerChange2: function (e) {
				this.projectTypeIndex = e.detail.value
				const projectType = this.projectTypes[this.projectTypeIndex];
				if (projectType.value == 0) {
					this.projectTypeFee = 10;
				} else if(projectType.value == 1){
					this.projectTypeFee = 15;
				} else if(projectType.value == 2){
					this.projectTypeFee = 30;
				}
				this.calculateFee();
			},
			bindDateChange: function(e) {
				this.date = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;

				return `${year}-${month}-${day}`;
			}
		}
	}
</script>

<style lang="scss">
	.container {
		width: 100%;
		height: 100vh;
		background-size: 100% 100%;
		background-color: #F8F8F8;
		padding-top: 15px;
	}

	.content {
		background-color: white;
		height: 150px;
		width: 100%;
	}

	.name {
		font-size: 20px;
		font-weight: 600;
		width: 200px;
		height: 40px;
	}

	.tips {
		font-size: 15px;
		font-weight: 400;
		color: #999999;
		margin-top: 15px;
	}

	.button-tips {
		margin-top: 35px;
		margin-left: 50px;
		height: 120px;
		font-size: 15px;
		font-weight: 400;
		color: #999999;
	}

	.title-tip {
		color: #333333;
		font-size: 18px;
		font-weight: 600;
		margin-bottom: 10px;
	}

	.jjtips {
		font-size: 15px;
		font-weight: 400;
		color: #999999;
		margin-left: 10px;
	}

	.jztips {
		color: #999999;
		font-size: 15px;
		font-weight: 400;
		width: 300px;
		margin-left: 15px;
	}

	.jzForm {
		background-color: white;
		margin-top: 15px;
		padding: 10px;
	}

	.formItem {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
	}

	.formLabel {
		width: 100px;
		font-size: 18px;
		// font-weight: 600;
	}

	.formInput {
		width: 200px;
		height: 30px;
		border-radius: 5px;
		padding-left: 50px;
		margin-top: 2px;
		line-height: 30px;
	}

	.btn-group {
		display: flex;
		justify-content: space-between;
		background-color: white;
		align-items: center;
		margin-top: 48px;
	}

	.left-aside {
		width: 150px;
		height: 40px;
		border-radius: 5px;
		text-align: center;
		line-height: 40px;
	}

	.right-content {
		width: 150px;
		height: 40px;
		background-color: #57A0FC;
		text-align: center;
		line-height: 40px;
		color: white;
	}

	page {
		.docterImg {
			margin: 25px 10px;
			width: 100px;
			height: 100px;
		}

		.right-aside {
			width: 270px;
			height: 120px;
			display: flex;
			float: right;
			// justify-content: space-around;
			flex-direction: column;
			margin-top: 20px;
			padding-left: 10px;
		}
	}
</style>